<template>
  <div class="login-container">
    <div class="title1">
      <div style="font-size: 40px"><h1>Learning Platform</h1></div>
      <div style="font-size: 23px">基于云原生的在线编程学习平台</div>
    </div>

    <el-card>
      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form" label-width="80px">
        <h1 class="title">邮箱验证码登录</h1>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="loginForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input v-model="loginForm.verificationCode" placeholder="请输入验证码" style="width: 190px;margin-right: 10px"></el-input>
          <el-button type="primary" style="width: 120px;" :disabled="emailValid" @click="sendVerificationCode">{{ countdown }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 100px" @click="login">登录</el-button>
        </el-form-item>
        <el-link @click="admin" style="float: right;margin-bottom: 10px">管理员登录</el-link>
      </el-form>
    </el-card>

  </div>
</template>

<script>
import { code, login } from '@/api/login'
import { setToken } from '@/utils/auth'
export default {
  name:"LoginIndex",
  data() {
    return {
      loginForm: {
        email: '2955356630@qq.com',
        verificationCode: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
        ],
        verificationCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      token:'',
      emailValid: false,
      countdown: '获取验证码',
      timer: null
    }
  },
  methods: {
    admin(){
      this.$router.push('/login/admin')
    },
    sendVerificationCode() {
      code(this.loginForm.email)

      this.emailValid = true;
      let count = 60;
      this.countdown = `${count}s`;
      this.timer = setInterval(() => {
        count--;
        if (count > 0) {
          this.countdown = `${count}s`;
        } else {
          clearInterval(this.timer);
          this.timer = null;
          this.countdown = '获取验证码';
          this.emailValid = false;
        }
      }, 1000);
    },
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          login({
            email: this.loginForm.email,
            verificationCode: this.loginForm.verificationCode
          }).then(data => {
            this.token = data.data
            if (this.token) {
              setToken(this.token)
              this.$router.push({ path: '/home' })
            }
          }).catch((res)=>{
            console.log(res)
          })

        } else {
          return false;
        }
      });
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  }
}
</script>

<style>
.title1{
  justify-content: center;
  align-items: center;
  color: white;
  display: flex;
  flex-direction: column;
  margin-right: 400px;
}
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 200px;
}

.login-form {
  width: 400px;
}

.title {
  font-size: 24px;
  margin-bottom: 20px;
  text-align: center;
}
</style>

